﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AppCommerciaux.Entity;
using AppCommerciaux.Views;
using System.Windows.Forms;
using System.Threading;



namespace AppCommerciaux.ViewModels
{
    public class CommandeViewModel : ViewModelBase
    {
        public CommandeViewModel()
        {
            try
            {
                this.LabelboutonSupprimer = "Supprimer";
                this.EnableButtonAjouter = false;
                Task.Factory.StartNew(() => LoadListCommande());
                Titre = "CommandeView";
                EnableButtonModifier = false;
                EnableButtonSupprimer = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        #region Methode

        public async void LoadListCommande()
        {
            try
            {
                await Task.Yield();
                Titre = "Chargement des commandes en cours";
                using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                {
                    var querySelect = from commandeclient in dc.commandeclient select commandeclient;
                    ListCommande = querySelect.ToList();
                }
                Titre = "CommandeView";
                this.EnableButtonAjouter = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SupprimerCommande()
        {
            try
            {
                //supprimer la selected commande
                using (Entity.liv3Entities1 dc = new Entity.liv3Entities1())
                {
                    if ((SelectedCommande.synchro == "") || (string.IsNullOrEmpty(SelectedCommande.synchro)) || (SelectedCommande.synchro == "OK"))
                    {
                        dc.commandeclient.Where(cc => cc.id == SelectedCommande.id).FirstOrDefault().synchro = "D";
                    }
                    else if (SelectedCommande.synchro == "D")
                    {
                        dc.commandeclient.Where(cc => cc.id == SelectedCommande.id).FirstOrDefault().synchro = "";
                    }
                    dc.SaveChanges();
                    //SelectedCommande.synchro = "d";
                    //commandeclient com = (from c in dc.commandeclient where c.id == SelectedCommande.id select c).First();
                    //com.synchro = "todelete";
                    //dc.SaveChanges();
                }

                // rafraichir commandeCLient
                App.Apv.LoadCommandeClientView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Declaration

        private List<commandeclient> _listCommande;
        public List<commandeclient> ListCommande
        {
            get { return _listCommande; }
            set
            {
                if (_listCommande != value)
                {
                    _listCommande = value;
                    OnPropertyChanged("ListCommande");
                }
            }
        }

        private commandeclient _selectedCommande;
        public commandeclient SelectedCommande
        {
            get { return _selectedCommande; }
            set
            {
                if (_selectedCommande != value)
                {
                    _selectedCommande = value;
                    if (value != null)
                    {
                        if("D".Equals(value.synchro)){
                            this.LabelboutonSupprimer = "Annuler la suppression";
                        }
                        else
                        {
                            this.LabelboutonSupprimer = "Supprimer";
                        }
                        Titre = "Commande n°"+value.id;
                        EnableButtonModifier = true;
                        EnableButtonSupprimer = true;
                        if (SelectedCommande.synchro == "N")
                        {
                            EnableButtonSupprimer = false;
                        }
                    }
                    OnPropertyChanged("SelectedCommande");
                }
            }
        }      

        private string _titre;
        public string Titre
        {
            get { return _titre; }
            set
            {
                if (_titre != value)
                {
                    _titre = value;
                    OnPropertyChanged("Titre");
                }
            }
        }

        private string _labelboutonSupprimer;
        public string LabelboutonSupprimer
        {
            get { return _labelboutonSupprimer; }
            set
            {
                if (_labelboutonSupprimer != value)
                {
                    _labelboutonSupprimer = value;
                    OnPropertyChanged("LabelboutonSupprimer");
                }
            }
        }

        private bool _enableButtonModifier;
        public bool EnableButtonModifier
        {
            get { return _enableButtonModifier; }
            set
            {
                if (_enableButtonModifier != value)
                {
                    _enableButtonModifier = value;
                    OnPropertyChanged("EnableButtonModifier");
                }
            }
        }
        private bool _enableButtonSupprimer;
        public bool EnableButtonSupprimer
        {
            get { return _enableButtonSupprimer; }
            set
            {
                if (_enableButtonSupprimer != value)
                {
                    _enableButtonSupprimer = value;
                    OnPropertyChanged("EnableButtonSupprimer");
                }
            }
        }
        
        private bool _enableButtonAjouter;
        public bool EnableButtonAjouter
        {
            get { return _enableButtonAjouter; }
            set
            {
                if (_enableButtonAjouter != value)
                {
                    _enableButtonAjouter = value;
                    OnPropertyChanged("EnableButtonAjouter");
                }
            }
        }

        #endregion

        #region Commande

        private RelayCommand _boutonAjouterCommande;
        public RelayCommand BoutonAjouterCommande
        {
            get
            {
                if (_boutonAjouterCommande == null)
                {
                    _boutonAjouterCommande = new RelayCommand(() => App.Apv.LoadEditCommandeClientView("Ajout", new commandeclient()));
                }
                return _boutonAjouterCommande;
            }
        }

        private RelayCommand _boutonModifierCommande;
        public RelayCommand BoutonModifierCommande
        {
            get
            {
                if (_boutonModifierCommande == null)
                {
                    _boutonModifierCommande = new RelayCommand(() => App.Apv.LoadEditCommandeClientView("Modification" , SelectedCommande));
                }
                return _boutonModifierCommande;
            }
        }

        private RelayCommand _boutonSupprimerCommande;
        public RelayCommand BoutonSupprimerCommande
        {
            get
            {
                if (_boutonSupprimerCommande == null)
                {
                    _boutonSupprimerCommande = new RelayCommand(() => SupprimerCommande());
                }
                return _boutonSupprimerCommande;
            }
        }
     


        #endregion
    }

}
